/*****************************************************************************
* File Name: motion_sensor.h
*
* Version: 1.0
*
* Description:
* This file contains API for motion sensor
*
* Note:
* Define DISABLE_MOTION_SENSOR in platform.h to disable motion sensor module
*
* Owner: KMVP
*
* Related Document:
* SCB component datasheet
*
* Hardware Dependency:
* MPU6500 Datasheet
*
* Code Tested With:
* 1. PSoC Creator 3.1 
* 2. ARM GCC 4.8.4
******************************************************************************
* Copyright (2015), Cypress Semiconductor Corporation.
******************************************************************************
* This software is owned by Cypress Semiconductor Corporation (Cypress) and is
* protected by and subject to worldwide patent protection (United States and
* foreign), United States copyright laws and international treaty provisions.
* Cypress hereby grants to licensee a personal, non-exclusive, non-transferable
* license to copy, use, modify, create derivative works of, and compile the
* Cypress Source Code and derivative works for the sole purpose of creating
* custom software in support of licensee product to be used only in conjunction
* with a Cypress integrated circuit as specified in the applicable agreement.
* Any reproduction, modification, translation, compilation, or representation of
* this software except as specified above is prohibited without the express
* written permission of Cypress.
*
* Disclaimer: CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH
* REGARD TO THIS MATERIAL, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 
* Cypress reserves the right to make changes without further notice to the
* materials described herein. Cypress does not assume any liability arising out
* of the application or use of any product or circuit described herein. Cypress
* does not authorize its products for use as critical components in life-support
* systems where a malfunction or failure may reasonably be expected to result in
* significant injury to the user. The inclusion of Cypress' product in a life-
* support systems application implies that the manufacturer assumes all risk of
* such use and in doing so indemnifies Cypress against all charges. Use may be
* limited by and subject to the applicable Cypress software license agreement.
*****************************************************************************/
#ifndef _MOTION_SENSOR_H_
#define _MOTION_SENSOR_H_

#include "platform.h"
#include "device.h"

#ifndef DISABLE_MOTION_SENSOR

#define AIR_MOTION_DEFAULT_GAIN_DELTA_X                 (10u)
#define AIR_MOTION_DEFAULT_GAIN_DELTA_Y                 (10u)
#define AIR_MOTION_DEFAULT_GYROOFFSET_X                 (0x0)
#define AIR_MOTION_DEFAULT_GYROOFFSET_Y                 (0x0)
#define AIR_MOTION_DEFAULT_GYROOFFSET_Z                 (0x0)
#define AIR_MOTION_DEFAULT_GYROSTATIC_NOISE             (16u)
#define AIR_MOTION_DEFAULT_STATIC_SAMPLES               (5u)
#define AIR_MOTION_DEFAULT_SWIPE_DISTMIN                (500u)
#define AIR_MOTION_DEFAULT_SWIPE_NOISE_MAX              (200u)
#define AIR_MOTION_DEFAULT_SAMPLES_GYRO_STARTUP         (16u)
#define AIR_MOTION_DEFAULT_CLICKSTILLSAMPLES            (50u)
#define AIR_MOTION_DEFAULT_CLICKSTILLTOLERANCE          (AirMotionNormal)
#define AIR_MOTION_DEFAULT_ISROLLCOMPENABLED            (1u)
#define AIR_MOTION_DEFAULT_ACCR_SENSITIVITY             (16386u)
#define AIR_MOTION_DEFAULT_GYRO_SENSITIVITY             (262u)

#define AIR_MOTION_REGULAR_STATIC_SAMPLES               (5u)

#define MOTION_SENSOR_GYRO_OFFSET_THRESHOLD_FLASH_WRITE (AIR_MOTION_DEFAULT_GYROSTATIC_NOISE*2)
/* Gesture Code is detected by the Motion sensor library */
#define MOTION_SENSOR_GESTURE_UP                        (0x40)
#define MOTION_SENSOR_GESTURE_DOWN                      (0x41)
#define MOTION_SENSOR_GESTURE_LEFT                      (0x42)
#define MOTION_SENSOR_GESTURE_RIGHT                     (0x43)
#define MOTION_SENSOR_GESTURE_CLOCKWISE                 (0x44)
#define MOTION_SENSOR_GESTURE_COUNTER_CLOCKWISE         (0x45)

/* Key Codes */
#define KEY_PAGE_UP                                     (0x4B)
#define KEY_PAGE_DOWN                                   (0x4E)
#define KEY_CTRL                                        (0x01)
#define KEY_PLUS                                        (0x57)
#define KEY_MINUS                                       (0x56)
    
#define NEXT_BUTTON                                     (0x08)
#define PREVIOUS_BUTTON                                 (0x10)

#define Motion_Sensor_IsCalibrationComplete()           (isCalibrationDone)
extern bool isCalibrationDone;

/*****************************************************************************
* Function Name: Motion_Sensor_IsActive()
******************************************************************************
* Summary:
* This function is used check if the motion sensor is active or not
*
* Parameters:
* None
*
* Return:
* bool - true if motion sensor is active and shows mouse pointer movement
*****************************************************************************/
extern bool Motion_Sensor_IsActive(void);

/*****************************************************************************
* Function Name: MotionSensor_IsMotionDetectedLowPower()
******************************************************************************
* Summary:
* This function is used to poll for the motion sensor activity in low power mode
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void MotionSensor_IsMotionDetectedLowPower(void);

/*****************************************************************************
* Function Name: Motion_Sensor_Set_Resolution()
******************************************************************************
* Summary:
* This function updates the resolution of mouse movement
*
* Parameters:
* gainResolutionIndex
* 
* Return:
* None
*
* Note:
* gainResolutionIndex can take value from 0,1,2,3. 
* Following are the values the resolution it takes
* -----------------------------------------
* gainResolutionIndex    |    Resolution  |
* -----------------------------------------
*         0              |      2         |
*         1              |      7         |
*         2              |      10        |
*         3              |      15        | (Full Resolution)
* -----------------------------------------
*****************************************************************************/
extern void Motion_Sensor_Set_Resolution(uint8 gainResolutionIndex);

/*****************************************************************************
* Function Name: Motion_Sensor_Start_Sampling()
******************************************************************************
* Summary:
* This function starts the sampling of motion sensor data
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Motion_Sensor_Start_Sampling(void);

/*****************************************************************************
* Function Name: Motion_Sensor_Stop_Sampling()
******************************************************************************
* Summary:
* This function is stops the sampling the motion sensor data
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Motion_Sensor_Stop_Sampling(void);

/*****************************************************************************
* Function Name: Motion_Sensor_Set_State()
******************************************************************************
* Summary:
* This function is used to set the state of the motion sensor in sampling / motion 
* detect mode
*
* Parameters:
* Device_State state - the state to set
*
* Return:
* None
*****************************************************************************/
extern void Motion_Sensor_Set_State(Device_State state);

/*****************************************************************************
* Function Name: Motion_Sensor_Poll()
******************************************************************************
* Summary:
* This function is used to check if motion sensor has mouse pointer movement
*
* Parameters:
* bool buttonChangedStatus - true if the button status has pressed
*
* Return:
* None
*****************************************************************************/
extern void Motion_Sensor_Poll(bool buttonChangedStatus);

/*****************************************************************************
* Function Name: Motion_Sensor_Get_Report()
******************************************************************************
* Summary:
* This function    uses the mouse position shift to update the HID report
* passed as parameter
*
* Parameters:
* report : pointer to the report to be updated
*
* Return:
* None
*****************************************************************************/
extern ReportType Motion_Sensor_Get_Report(Report *report);

/*****************************************************************************
* Function Name: Motion_Sensor_Stop()
******************************************************************************
* Summary:
* This function stops motion sensor. 
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Motion_Sensor_Stop(void);

/*****************************************************************************
* Function Name: Motion_Sensor_Init()
******************************************************************************
* Summary:
* This function initializes motion sensor. 
*
* Parameters:
* None
*
* Return:
* None
*****************************************************************************/
extern void Motion_Sensor_Init(void);

#else
#define Motion_Sensor_Init()
#define Motion_Sensor_Start_Sampling()
#define Motion_Sensor_Set_State(x)
#define Motion_Sensor_Poll(buttonChangedStatus)
#define Motion_Sensor_Get_Report(x)              NO_REPORT
#define Motion_Sensor_Stop_Sampling(void)
#define Motion_Sensor_Stop()
#define Motion_Sensor_IsActive()                 false
#define MotionSensor_IsMotionDetectedLowPower()
#define Motion_Sensor_IsCalibrationComplete()    true
#define Motion_Sensor_Set_Resolution(index)
#endif /* DISABLE_MOTION_SENSOR */

#endif /* _MOTION_SENSOR_H_ */
